package com.flushbonading.service.impl;


import com.flushbonading.mapper.ScoreMapper;
import com.flushbonading.mapper.TeacherMapper;
import com.flushbonading.pojo.Course;
import com.flushbonading.mapper.CourseMapper;
import com.flushbonading.pojo.dto.CourseDTO;
import com.flushbonading.pojo.dto.CourseMessageDTO;
import com.flushbonading.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flushbonading.util.CourseTypeUtil;
import com.flushbonading.util.DirectionUtil;
import com.flushbonading.util.InspectionTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlx
 * @since 2021-04-07
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public List<CourseMessageDTO> getAllMessage() {
        return courseMapper.getAllMessage();
    }

    @Override
    public List<CourseMessageDTO> getCourseByStudyDirection(int study_direction_id) {
        return courseMapper.getCourseByStudyDirection(study_direction_id);
    }

    @Override
    public int insertACourse(int index,String course_name, String teacherName, String type,Double scorePercentage) {
        CourseMessageDTO build = CourseMessageDTO.builder()
                .course_id(index)
                .course_name(course_name)
                .teacherName(teacherName)
                .study_direction_id(DirectionUtil.directionID(type))
                .scorePercentage(scorePercentage)
                .build();
        return courseMapper.insertACourse(build);
    }

    @Override
    public int deleteByCourseID(int course_id) {
        return courseMapper.deleteByCourseID(course_id);
    }

    @Override
    public int getLastCourseID() {
        return courseMapper.getLastCourseID();
    }

    @Override
    public Integer counts(String course_name) {
        return courseMapper.counts(course_name);
    }

    @Override
    public CourseDTO getDetailsByID(int course_id) {
        CourseDTO detailsByID = courseMapper.getDetailsByID(course_id);
        Double exam_score = detailsByID.getExam_score();
        DecimalFormat df=new DecimalFormat("0.0");
        detailsByID.setUsually_score(Double.valueOf(df.format(1-exam_score)));
        return detailsByID;
    }

    @Override
    public int updateDetailsByID(int course_id, CourseDTO courseDTO) {
        int teacherID = teacherMapper.getIdByTeacherName(courseDTO.getTeacher_name());
        int i = courseMapper.updateDetailsByID(course_id, courseDTO.getCourse_name(), teacherID, courseDTO.getCredit(), CourseTypeUtil.courseTypeID(courseDTO.getCategory()), InspectionTypeUtil.InspectionTypeID(courseDTO.getAssessment_method()), courseDTO.getExam_score());
        return i;
    }

}
